﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class guideAction : CWebBase
{
    public CDatabase _db = new CDatabase();

    protected void Page_Load(object sender, EventArgs e)
    {
        // 어떤 action ?
        String action = CheckNull(Request["action"]);

        _db.GetConnection();

        // get guide datas (by LevelId)
        if (action == "getGuide")
        {
            String LevelId = CheckNull(Request["LevelId"]);
            LevelId = RemoveSpecialChars(LevelId);

            if(isEmpty(LevelId)){
                return;
            }

            Hashtable data = new Hashtable();

            String sql = "Select " +
                    " LevelId " +
                    ", StLenMin, StLenMax " +
                    ", AvgLenMin, AvgLenMax " +
                    ", StLevelMin, StLevelMax " +
                    ", DisScope1, DisMin1, DisMax1 " +
                    ", DisScope2, DisMin2, DisMax2 " +
                    ", DisScope3, DisMin3, DisMax3 " +
                    ", ModDate, ModUser From ANALYZE_WORD_GUIDELINE Where LevelId = '" + LevelId + "'";

            SqlDataReader dr = _db.GetQueryResult(sql);

            if (dr.HasRows)
            {
                dr.Read();
                data.Add("LevelId", dr["LevelId"].ToString());
                data.Add("StLenMin", dr["StLenMin"].ToString());
                data.Add("StLenMax", dr["StLenMax"].ToString());
                data.Add("AvgLenMin", dr["AvgLenMin"].ToString());
                data.Add("AvgLenMax", dr["AvgLenMax"].ToString());
                data.Add("StLevelMin", dr["StLevelMin"].ToString());
                data.Add("StLevelMax", dr["StLevelMax"].ToString());
                data.Add("DisScope1", dr["DisScope1"].ToString());
                data.Add("DisMin1", dr["DisMin1"].ToString());
                data.Add("DisMax1", dr["DisMax1"].ToString());
                data.Add("DisScope2", dr["DisScope2"].ToString());
                data.Add("DisMin2", dr["DisMin2"].ToString());
                data.Add("DisMax2", dr["DisMax2"].ToString());
                data.Add("DisScope3", dr["DisScope3"].ToString());
                data.Add("DisMin3", dr["DisMin3"].ToString());
                data.Add("DisMax3", dr["DisMax3"].ToString());
            }
            dr.Close();

            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(data));
            return;
        }

        

        // 가이드 저장
        if (action == "saveGuide")
        {
            Hashtable result = new Hashtable();
            String succ = "Y";

            String LevelId = CheckNull(Request["LevelId"]);

            String StLenMin = CheckNull(Request["StLenMin"]);
            String StLenMax = CheckNull(Request["StLenMax"]);

            String AvgLenMin = CheckNull(Request["AvgLenMin"]);
            String AvgLenMax = CheckNull(Request["AvgLenMax"]);

            String StLevelMin = CheckNull(Request["StLevelMin"]);
            String StLevelMax = CheckNull(Request["StLevelMax"]);

            String DisScope1 = CheckNull(Request["DisScope1"]);
            String DisMin1 = CheckNull(Request["DisMin1"]);
            String DisMax1 = CheckNull(Request["DisMax1"]);

            String DisScope2 = CheckNull(Request["DisScope2"]);
            String DisMin2 = CheckNull(Request["DisMin2"]);
            String DisMax2 = CheckNull(Request["DisMax2"]);

            String DisScope3 = CheckNull(Request["DisScope3"]);
            String DisMin3 = CheckNull(Request["DisMin3"]);
            String DisMax3 = CheckNull(Request["DisMax3"]);

            LevelId = RemoveSpecialChars(LevelId);

            if (isEmpty(LevelId)) {
                return;
            }

            try
            {
                String sql = "Delete From ANALYZE_WORD_GUIDELINE where LevelId = '" + LevelId + "'";
                SqlDataReader dr = _db.GetQueryResult(sql);
                dr.Close();

                sql = "Insert into ANALYZE_WORD_GUIDELINE ( LevelId, StLenMin, StLenMax, AvgLenMin, AvgLenMax, StLevelMin, StLevelMax," +
                       " DisScope1, DisMin1, DisMax1, DisScope2, DisMin2, DisMax2, DisScope3, DisMin3, DisMax3 ) values (" +
                       "'" + LevelId + "','" + StLenMin + "','" + StLenMax + "','" + AvgLenMin + "','" + AvgLenMax + "','" + StLevelMin + "','" + 
                       StLevelMax + "','" + DisScope1 + "','" + DisMin1 + "','" + DisMax1 + "','" + DisScope2 + "','" + DisMin2 + "','" + DisMax2 + "','" + 
                       DisScope3 + "','" + DisMin3 + "','" + DisMax3 + "')";

                SqlDataReader dr2 = _db.GetQueryResult(sql);
                dr2.Close();
            }
            catch (Exception ex)
            {
                succ = "N";
            }
            finally
            {
                result.Add("succ", succ);
            }
            Response.AddHeader("'Content-type", "application/json");
            Response.Write(getJson(result));
        }
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        _db.ReleaseConnection();
    }
    
}
